﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.Hosting;
    using ufida.u9.ait.openapi.utils;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.Data;

    /// <summary>
    /// PrintExportSV partial 
    /// </summary>	
	public partial class PrintExportSV 
	{	
		internal BaseStrategy Select()
		{
			return new PrintExportSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class PrintExportSVImpementStrategy : BaseStrategy
	{
		public PrintExportSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			PrintExportSV bpObj = (PrintExportSV)obj;

			Dictionary<String,Object> ret = new Dictionary<String,Object>();
            try
            {
				if (bpObj.Data == null) throw new Exception("data为空");

                //因为打印输出中框架代码中需要用到HttpContext,这里临时添加一个
                TextWriter tw = new StringWriter();
                HttpWorkerRequest wr = new SimpleWorkerRequest("default.htm", "", tw);
                HttpContext.Current = new HttpContext(wr);
				//输出pdf
                string filePath = PrintUtils.ExportPDF(bpObj.Data.PrintCatalog, bpObj.Data.PrintTmplID, 
					bpObj.Data.DataView, bpObj.Data.DataPath);
				//拷贝pdf到指定目录
				byte[] bytes = FileUtils.ReadBinFile(filePath);
                string fileName = Path.GetFileName(filePath);
                string filePath1 = FileUtils.WriteU9BinFile(bpObj.Data.SaveDir, fileName, bytes);

				//处理返回
				ret.Add("fileName", fileName);
				ret.Add("filePath", filePath1);
				ret.Add("url", bpObj.Data.SaveDir + "/" + fileName);
                return ResultUtils.success(bpObj, bpObj, ret);
            }
			catch (Exception ex)
			{
                return ResultUtils.error(bpObj, bpObj, ex);
            }
			
		}		
	}

	#endregion
	
	
}